package com.white.algebra.service.impl;

import Jama.Matrix;
import com.white.algebra.service.CalculateService;
import com.white.algebra.utils.StringUtils;

import java.util.Arrays;

public class CalculateServiceImpl implements CalculateService {

    /**
     * 1
     * 行列式计算
     *
     * @param textA
     * @return
     */
    @Override
    public String determinantCalculate(String textA) {
        Matrix matrix=new Matrix(StringUtils.stringToDoubleArray(textA));
        return matrix.det()+"";
    }

    /**
     * 2
     * 解线性方程组
     *
     * @param textA
     * @return
     */
    @Override
    public String solveLinearEquations(String textA,String textB) {
        Matrix matrixA=new Matrix(StringUtils.stringToDoubleArray(textA));
        Matrix matrixB=new Matrix(StringUtils.stringToDoubleArray(textB));
        Matrix matrixEnd=matrixA.solve(matrixB);
        double[][] matrixText=matrixEnd.getArrayCopy();
        return StringUtils.doubleToStringArray(matrixText);
    }

    /**
     * 3
     * 矩阵加法
     *
     * @param textA
     * @param textB
     * @return
     */
    @Override
    public String matrixAddition(String textA, String textB) {
        Matrix matrixA=new Matrix(StringUtils.stringToDoubleArray(textA));
        Matrix matrixB=new Matrix(StringUtils.stringToDoubleArray(textB));
        Matrix matrixEnd=matrixA.plus(matrixB);
        double[][] matrixText=matrixEnd.getArrayCopy();
        return StringUtils.doubleToStringArray(matrixText);
    }

    /**
     * 4
     * 矩阵乘法
     *
     * @param textA
     * @param textB
     * @return
     */
    @Override
    public String matrixMultiplication(String textA, String textB) {
        Matrix matrixA=new Matrix(StringUtils.stringToDoubleArray(textA));
        Matrix matrixB=new Matrix(StringUtils.stringToDoubleArray(textB));
        Matrix matrixEnd=matrixA.times(matrixB);
        double[][] matrixText=matrixEnd.getArrayCopy();
        return StringUtils.doubleToStringArray(matrixText);
    }

    /**
     * 5
     * 矩阵转置
     *
     * @param textA
     * @return
     */
    @Override
    public String matrixTranspose(String textA) {
        Matrix matrix=new Matrix(StringUtils.stringToDoubleArray(textA));
        Matrix end=matrix.transpose();
        double[][] matrixText=end.getArrayCopy();
        return StringUtils.doubleToStringArray(matrixText);
    }

    /**
     * 6
     * 逆矩阵
     *
     * @param textA
     * @return
     */
    @Override
    public String inverseMatrix(String textA) {
        Matrix matrix=new Matrix(StringUtils.stringToDoubleArray(textA));
        Matrix end=matrix.inverse();
        double[][] matrixText=end.getArrayCopy();
        return StringUtils.doubleToStringArray(matrixText);
    }

    /**
     * 7
     * 矩阵的秩
     *
     * @param textA
     * @return
     */
    @Override
    public String matrixRank(String textA) {
        Matrix matrix=new Matrix(StringUtils.stringToDoubleArray(textA));
        return matrix.rank()+"";
    }

    /**
     * 8
     * 线性相关的判断
     *
     * @param textA
     * @return
     */
    @Override
    public String linearCorrelation(String textA) {
        double[][] a=StringUtils.stringToDoubleArray(textA);
        Matrix matrix=new Matrix(a);
        int i=matrix.rank();
        if(i<a.length){
            return "线性相关";
        }else {
            return "线性无关";
        }

    }

    /**
     * 9
     * 矩阵的特征值
     *
     * @param textA
     * @return
     */
    @Override
    public String matrixEigenvalues(String textA) {
        Matrix matrix=new Matrix(StringUtils.stringToDoubleArray(textA));
        double[] end=matrix.eig().getRealEigenvalues();
        String endString="";
        for (double d:end
             ) {
            endString=endString+d+" ";
        }
        return endString;
    }

    /**
     * 10
     * 矩阵是否相似
     *
     * @param textA
     * @param textB
     * @return
     */
    @Override
    public String matrixSimilarity(String textA, String textB) {
        Matrix matrixA=new Matrix(StringUtils.stringToDoubleArray(textA));
        Matrix matrixB=new Matrix(StringUtils.stringToDoubleArray(textB));
        double[] endA=matrixA.eig().getRealEigenvalues();
        double[] endB=matrixB.eig().getRealEigenvalues();
        if(Arrays.equals(endA,endB)){
            return "相似";
        }else{
            return "不相似";
        }
    }
}
